/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "TransactionViewWidget.h"

#include <QtCore/QTime>
#include <QtGui/QEvent.h>
#include <QtGui/QMessageBox>

#include <QtGui/QGridLayout>
#include <QtGui/QPushButton>

#include <QtGui/QHBoxLayout>
#include <QtGui/QTextBlock>
#include <QtGui/QLabel>

#include "QueryManager.h"
#include "PortfolioManager.h"
#include "Utilities.h"

namespace QSThink
{

	TransactionViewWidget::TransactionViewWidget(QWidget *parent,Qt::WindowFlags flag)
		:QWidget(parent,flag),_inited(false)
	{
		
		setAcceptDrops(true);
		setContextMenuPolicy(Qt::DefaultContextMenu );

		setFocusPolicy(Qt::WheelFocus);
		setMouseTracking(true);
		//setAttribute(Qt::WA_NoBackground);
		//setAttribute(Qt::WA_PaintOnScreen);
		setTimer();

		setMinimumSize(600,400);
		

		_dataTable=new QTableWidget(0,5);

		QStringList headers;
		headers  << "Time"  << "Price"
			<<"Volume"<<"Amount"<< "Buy/Sell";


		_dataTable->setHorizontalHeaderLabels(headers);


		QLabel* labelSymbol=new QLabel(tr("Symbol:"));
		QLabel* labelDate=new QLabel(tr("Date:"));

		_lineEditSymbol=new QLineEdit();

		QPushButton *btnSymbol = new QPushButton(tr("Search"));

		_dateEdit=new QDateEdit();
		QDate currentDate=QDate::currentDate();
		_dateEdit->setDate(currentDate);
		_dateEdit->setCalendarPopup(true);

		QGridLayout *layout = new QGridLayout;

		int row=0;
		layout->addWidget(labelDate,row,0);
		layout->addWidget(_dateEdit,row,1);
		//row++;
		layout->addWidget(labelSymbol,row,2);
		layout->addWidget(_lineEditSymbol,row,3);
		layout->addWidget(btnSymbol,row,4);

		row++;

		layout->addWidget(_dataTable, row, 0,1,5);

		this->setLayout(layout);



		connect(btnSymbol, SIGNAL(clicked()), this, SLOT(symbolBtnClicked()));


		loadPortfolio();
	}

	void TransactionViewWidget::setTimer()
	{
		startTimer(50);//100 msec 0.1 sec 10frame/second max frame rate
		
		QTime time = QTime::currentTime();
		unsigned int curtime = time.hour() * 60 + time.minute();
		curtime = (curtime * 60) + time.second();
		curtime = (curtime * 1000) + time.msec();
		_lastTime=curtime;
	}

	 void TransactionViewWidget::timerEvent(QTimerEvent *evt)
	 {
		 if(!this->isActiveWindow()){
			 return;
		 }

		 int id=evt->timerId();

		QTime time = QTime::currentTime();
		unsigned int curtime = time.hour() * 60 + time.minute();
		curtime = (curtime * 60) + time.second();
		curtime = (curtime * 1000) + time.msec();

		unsigned int deltaT = curtime - _lastTime;
        _lastTime = curtime;

		//update by frame

	 }


	TransactionViewWidget::~TransactionViewWidget()
	{
		destroy();
	}
	
	void TransactionViewWidget::symbolBtnClicked()
	{
		QString symbolText=_lineEditSymbol->text();

		bool success=false;

		int symbol=symbolText.toInt(&success);

		if(!success){
			return;//just return;
		}
		assert(success);


		std::map<int,StockInfo>::iterator it=_stockInfoMap.find(symbol);
		if(it!=_stockInfoMap.end()){
			return;//just return for find(already in )
		}


		QDate currentDate=_dateEdit->date();
		QDate dateStart(1900,1,1);
		int day=dateStart.daysTo(currentDate);

		//QueryManager::getSingleton().getDayTransactions(day,symbol,this);

		//PortfolioManager::getSingleton().addSymbol(symbol);
		//


	}

	void TransactionViewWidget::OnStockInfoReceived(StockInfo info)
	{
	}

	void TransactionViewWidget::loadPortfolio()
	{
		
	}

	void TransactionViewWidget::OnDayTransactionReceived( Transaction1Day* trans )
	{
		
		QString dateStr=Utilities::getDateStrByDay(trans->day());

		int cnt=trans->getTransCount();

		_dataTable->setRowCount(cnt);
		for (int i=0;i<cnt;i++){
			TransactionSingle* t=trans->getTransaction(i);
			addTransaction(i,t);

		}
	}

	void TransactionViewWidget::addTransaction(int index, TransactionSingle* t )
	{
		//insert row is very time waste why?
		//_dataTable->insertRow(_dataTable->rowCount());
		//int row=_dataTable->rowCount()-1;

		int time=t->time;
		QString timeStr=Utilities::getTimeStr(time);

		float price=(float)t->price/100.0f;
		int volume=t->volume;
		int amount=t->amount;

		QString type;
		switch (t->flag)
		{
		case 0:
			type="Buy";
			break;
		case 1:
			type="Sell";
			break;
		case 2:
		default:
			type="Neutral ";
			break;
		}


		//headers  << "Time"  << "Price"<<"Volume"<<"Amount"<< "Buy/Sell";
		///*
		QTableWidgetItem *timeItem = new QTableWidgetItem(timeStr);
		QTableWidgetItem *priceItem = new QTableWidgetItem(QString::number(price));
		QTableWidgetItem *volumeItem = new QTableWidgetItem(QString::number(volume));
		QTableWidgetItem *amountItem = new QTableWidgetItem(QString::number(amount));
		QTableWidgetItem *typeItem = new QTableWidgetItem(type);
		
		_dataTable->setItem(index,0, timeItem);
		_dataTable->setItem(index,1, priceItem);
		_dataTable->setItem(index,2, volumeItem);
		_dataTable->setItem(index,3, amountItem);
		_dataTable->setItem(index,4, typeItem);
		//*/

	}

}
